<?php

namespace App\Admin\Controllers;

use App\Models\User;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use \Log;
use \Cache;

class UserController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('用户管理');
            $content->description('用户列表');

            $content->body($this->grid());
        });
    }

    /**
     * Show interface.
     *
     * @param mixed   $id
     * @param Content $content
     * @return Content
     */
    public function show($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('用户管理');
            $content->description('编辑用户');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('用户管理');
            $content->description('编辑用户');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('用户管理');
            $content->description('添加用户');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(User::class, function (Grid $grid) {
            $grid->id('用户ID')->sortable();
            $grid->info()->nickname('名称');
            $grid->phone('手机号');


            $grid->filter(function ($filter) {
                // 在这里添加字段过滤器
                $filter->equal('id', '用户ID');
                $filter->equal('name', '名称');
                $filter->equal('phone', '手机号');
            });

        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(User::class, function (Form $form) {
            $form->tab('基础信息', function ($form) {
                $form->display('id', '用户ID');
                $form->text('name','用户名称');
                $form->text('phone','手机号');
                $form->text('email','email');
                $form->display('created_at', '创建时间');
                $form->display('updated_at', '更新时间');
            })->tab('补充信息', function ($form) {
                $form->hasMany('info_admin','详细信息', function (Form\NestedForm $form) {
                    $form->display('user_id','id');
                    $form->image('head_image','头像')->uniqueName();
                    $form->multipleImage('album')->uniqueName();
                    $form->text('nickname','昵称');
                    $form->radio('sex','性别')->options(['1' => '男', '2'=> '女']);
                    $form->datetime('birth_day','生日');
                    $form->text('height','身高,单位厘米');
                    $form->text('weight','体重,KG');
                    $form->text('income','收入,元');
                    $form->text('province','城市名称，严格:河南省');
                    $form->text('city','城市名称，严格:郑州市');
                    $form->text('county','区，严格:金水区');
                    $form->text('address','地址');
                    $form->text('education','学历');
                    $form->radio('if_married','是否结婚')->options(['1' => '未婚', '2'=> '已婚']);
                    $form->radio('if_child','是否有小孩')->options(['1' => '无', '2'=> '有']);
                    $form->radio('if_want_child','是否想有小孩')->options(['1' => '无', '2'=> '有']);
                    $form->radio('car','是否有车')->options(['1' => '无', '2'=> '有']);
                    $form->radio('smoke','是否抽烟')->options(['1' => '无', '2'=> '有']);
                    $form->radio('drink','是否喝酒')->options(['1' => '无', '2'=> '有']);
                    $form->text('occupation','职业');
                    $form->text('house','是否买房');
                    $form->text('house','是否买房');
                    $form->text('come_from','籍贯');
                    $form->text('shape','体型');
                    $form->text('constellation','星座');
                    $form->text('nation','民族');
                    $form->text('when_married','何时结婚');
                    $form->text('soliloquy','内心独白');
                    $form->datetime('member_expire','会员过期时间');
                    $form->radio('online','是否在线')->options(['1' => '在线','0' => '离线']);
                    $form->text('occupation_string','occupation_string');
                    $form->text('income_string','income_string');
                    $form->text('marks','marks');
                    $form->text('vip','vip');
                    $form->text('secret_info','小秘密');
                });
            });

        });
    }
}
